<template>
  <div class="scheduling_container">
    <el-button
      v-preventReClick
      class="flex_start"
      @click="showDialog(1)"
      type="primary"
      size="small"
    >添加学期
    </el-button
    >
    <div class="flex_center">
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="title"
        :visible.sync="dialogVisible"
        width="560px"
        :before-close="handleClose"
      >
        <el-form
          :model="formSemester"
          :rules="rules"
          ref="semesterForm"
          label-width="82px"
          class="demo-ruleForm"
        >
          <el-form-item label="学期" prop="semesterName">
            <el-input
              style="width: 334px"
              class="semesterName"
              clearable
              v-model="formSemester.semesterName"
              placeholder="请输入学期名称"
            ></el-input>
          </el-form-item>
          <div style="display: flex">
            <el-form-item label="学期时间" prop="beginTime">
              <el-date-picker
                style="width: 150px"
                class="date_picker"
                :picker-options="pickerOptionsEnd"
                v-model="formSemester.beginTime"
                type="date"
                clearable
                placeholder="开始日期"
              >
              </el-date-picker>
            </el-form-item>
            <span style="margin: 7px 10px 0">至</span>
            <el-form-item prop="endTime" label-width="0px">
              <el-date-picker
                style="width: 150px"
                class="date_picker"
                :picker-options="pickerOptionsStart"
                v-model="formSemester.endTime"
                type="date"
                clearable
                placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </div>
        </el-form>
        <div class="dialog_footer">
          <el-button v-preventReClick @click="handleClose">取 消</el-button>
          <el-button
            v-preventReClick
            type="primary"
            @click="submitForm('semesterForm')"
          >确 定
          </el-button
          >
        </div>
      </el-dialog>
      <div class="scheduling_content" v-if="semesterList.length > 0">
        <div class="content_info" v-loading="loaading">
          <div
            @click="routeTo(item)"
            class="content_info_left"
            v-for="(item, index) in semesterList"
            :key="index"
          >
            <div class="content_info_left_text">{{ item.semesterName }}</div>
            <div class="content_info_left_time">
              {{ item.beginTime | fammterTime }} ~
              {{ item.endTime | fammterTime }}
            </div>
            <div class="btns">
              <i @click.stop="showDialog(2, item)" class="el-icon-edit"></i>
              <img
                @click.stop="delSemester(item.semesterId)"
                class="del_img"
                src="@/assets/images/del_img.png"
                alt="加载失败"
              />
            </div>
          </div>
        </div>
      </div>
      <div class="empty_box" v-if="semesterList.length == 0">
        <el-empty description="暂无数据"></el-empty>
      </div>
    </div>
  </div>
</template>

<script>
import { addSchoolSemester, deleteSchoolSemester, getSchoolSemesterList, updateSchoolSemester } from '@/api/schoolSemester/schoolSemester'

export default {
  name: 'courseScheduling',
  data() {
    return {
      loaading: false,
      semesterList: [],
      title: '添加学期',
      dialogVisible: false,
      pickerOptionsStart: {
        // 时间只能大于startTime
        disabledDate: (time) => {
          if (!this.formSemester.beginTime) {
            return
          }
          return (
            time.getTime() < new Date(this.formSemester.beginTime).getTime()
          )
        }
      },
      pickerOptionsEnd: {
        // 时间只能小于startTime
        disabledDate: (time) => {
          if (!this.formSemester.endTime) {
            return
          }
          return time.getTime() > new Date(this.formSemester.endTime).getTime()
        }
      },
      formSemester: {
        semesterName: '',
        beginTime: '',
        endTime: ''
      },
      rules: {
        semesterName: [
          { required: true, message: '请输入学期名称', trigger: 'blur' }
        ],
        beginTime: [
          { required: true, message: '开始日期不能为空', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '结束日期不能为空', trigger: 'change' }
        ]
      }
    }
  },
  filters: {
    fammterTime(val) {
      if (!val) {
        return ''
      }
      let d = new Date(val)
      let year = d.getFullYear()
      let month = d.getMonth() + 1
      let date = d.getDate()
      return `${year}-${month < 10 ? '0' + month : month}-${
        date < 10 ? '0' + date : date
      }`
    }
  },
  activated() {
  },
  created() {
    this.getSemesterList()
  },
  methods: {
    routeTo(item) {
      this.$router.push({
        path: '/course/addScheduling',
        query: {
          id: item.semesterId,
          semesterName: item.semesterName
        }
      })
    },
    async getSemesterList() {
      this.loaading = true
      let res = await getSchoolSemesterList()
      if (res) {
        this.loaading = false
      } else {
        this.loaading = false
      }
      this.semesterList = res.rows
    },
    submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          let params = {
            ...this.formSemester
          }
          if (this.title == '添加学期') {
            let res = await addSchoolSemester(params)
            const { code } = res
            if (code == 200) {
              this.$message.success('添加成功')
              this.$refs['semesterForm'].resetFields()
              this.dialogVisible = false
              this.getSemesterList()
            }
          } else {
            console.log(params, 'params')
            let res = await updateSchoolSemester(params)
            const { code } = res
            if (code == 200) {
              this.$message.success('修改成功')
              this.$refs['semesterForm'].resetFields()
              this.dialogVisible = false
              this.getSemesterList()
            }
          }
        } else {
          return false
        }
      })
    },
    showDialog(type, item) {
      this.title = type == 1 ? '添加学期' : '编辑学期'
      if (type == 2) {
        this.formSemester.semesterId = item.semesterId
        this.formSemester.semesterName = item.semesterName
        console.log(
          this.$options.filters['fammterTime'](item.beginTime),
          this.$options.filters['fammterTime'](item.endTime)
        )
        this.formSemester.beginTime = item.beginTime
        this.formSemester.endTime = item.endTime
      }
      this.dialogVisible = true
    },
    handleClose() {
      this.formSemester = {
        semesterName: '',
        beginTime: '',
        endTime: ''
      }
      this.$refs['semesterForm'].resetFields()
      this.dialogVisible = false
    },
    delSemester(id) {
      this.$confirm('确认要删除该学期吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          let res = await deleteSchoolSemester(id)
          const { code } = res
          if (code == 200) {
            this.$message.success('删除成功！')
            this.getSemesterList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped lang="scss">
.flex_center {
  display: flex;
  justify-content: center;
}

.flex_clomn_center {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-right: 60px;
  box-sizing: border-box;
}

.scheduling_container {
  width: 100%;
  padding: 15px 20px;

  .scheduling_content {
    width: 1000px;

    .content_info {
      cursor: pointer;
      margin-top: 26px;
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-wrap: wrap;

      &_left {
        margin-bottom: 60px;
        position: relative;
        width: 460px;
        height: 200px;
        border: 2px solid rgba(22, 185, 144, 1);
        border-radius: 6px;
        color: rgba(22, 185, 144, 1);
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        &_text {
          font-size: 30px;
          font-weight: 500;
          color: rgba(22, 185, 144, 1);
        }

        &_time {
          font-size: 20px;
          font-weight: 400;
          color: rgba(22, 185, 144, 1);
          margin-top: 16px;
        }
      }

      &_left:hover {
        .btns {
          display: flex;
        }
      }
    }
  }
}

.btns {
  display: none;
  position: absolute;
  right: 20px;
  top: 20px;

  .el-icon-edit {
    cursor: pointer;
    font-size: 16px;
  }

  .del_img {
    cursor: pointer;
    width: 16px;
    height: 16px;
    object-fit: cover;
    margin-left: 10px;
  }
}

.semesterName {
  width: 314px;
}

.dialog_footer {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.empty_box {
  width: 100%;
  padding: 10vh 0;
  box-sizing: border-box;
  height: 680px;
}

.date_picker {
  width: 140px;
}
</style>
